<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitUserCard extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('card_assets', function (Blueprint $table) {
            $table->comment = '道具卡资产表';
            
            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('target_id')->index()->comment('用户id'); 
            $table->string('target_type', 20)->index()->comment('链接类型');

            $table->string('name', 30)->index()->comment('卡券名');
            $table->string('type', 20)->index()->comment('卡券代码'); // 财产代码

            $table->integer('precision')->unsigned()->default(0)->comment('精度，默认为0。 即单位为1'); // 精度，默认为0。 即单位为1

            $table->bigInteger('balance')->unsigned()->default(0)->index()->comment('数量'); // 余额
            $table->bigInteger('frozen')->unsigned()->default(0)->index()->comment('冻结的数量'); // 余额

            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('card_asset_records', function (Blueprint $table) {
            $table->comment = '卡券资产流水表';
            
            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('card_asset_id')->unsigned()->index()->comment('资产id'); // 财产类型id

            $table->integer('order_id')->unsigned()->nullable()->comment('订单id'); // order id

            $table->bigInteger('before')->unsigned()->comment('变动前余额'); 
            $table->bigInteger('after')->unsigned()->comment('变动后余额'); 
            $table->bigInteger('value')->unsigned()->comment('数额'); 

            $table->boolean('type')->default(1)->index()->comment('0为流出   1流入');   // 0为流出   1流入

            $table->string('description', 255)->comment('流水描述'); 

            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('card_assets');
        Schema::dropIfExists('card_asset_records');
    }
}
